問題描述
刪除文檔中的父對象 (Remove parent object in document)
我有一個卡片組列表,每組都有一組卡片對象。我創建了一個管道,它使用所有卡片創建一個新集合。當前它插入卡片對象而不是對象的每個值。
當前管道:
pipeline = [
{"$match": {"cards": {"$exists": True}}},
{"$project": {"cards": 1, "_id": 0} },
{"$project": {"cards.foreignData": 0, "_id": 0}},
{"$unwind": "$cards"},
#{"$set": {"_id": "cards.uuid"}},
{"$out": "cards" }
]
result = database.card_sets_source.aggregate(pipeline)
當前輸出如下所示:
_id: <random ID>
cards: Object
artist: "Artist name",
...: "...",
number: 1
我想要的輸出看起來像這樣:
_id: <random ID>
arstist: "Arist name",
...: "...",
number: 1
我缺少什麼聚合函數?
參考解法
方法 1:
It requires to replace object in root and then out to another collection,
$replaceRoot
to replacecards
object in root
</ul>
pipeline = [
# .... your pipeline stages
{ "$replaceRoot": { "newRoot": "$cards" } },
{"$out": "cards" }
]
(by In0cenT、turivishal)